package com.model.speelveld;

import java.util.ArrayList;
import android.os.CountDownTimer;
import com.model.speelveld.monsters.Monster;
import com.view.speelveld.GameObject;
import com.view.speelveld.GameView;


public abstract class Tower extends GameObject   {

	private int positionX;
	private int positionY;
	private int radius;
	private CountDownTimer reloadTimer;
	private int value;
	private int level;
	private int damage;
	private ArrayList<Path> rangePath;
	private GameObject[][] gameboard;
	
	
	public Tower(int positionX ,int positionY , int radius ,  long fireSpeed , int value, int damage, int TowerType , GameObject [][] gameboard)
	{
		super(TowerType);
		this.positionX = positionX;
		this.positionY = positionY;
		this.radius = radius;
		this.level = 1;
		this.reloadTimer = new CountDownTimer(fireSpeed, 250) {
			
			@Override
			public void onTick(long millisUntilFinished) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				shoot();
			}
		}.start();
		this.value = value;
		this.damage = damage;
		this.rangePath = new ArrayList<Path>();
		this.gameboard = gameboard;
		
	}
	public void setRangePath(ArrayList<Path> rangePath)
	{
		this.rangePath = rangePath;
		
		
	}
	public int getPositionX() {
		return positionX;
	}

	public void setPositionX(int positionX) {
		this.positionX = positionX;
	}

	public int getPositionY() {
		return positionY;
	}

	public void setPositionY(int positionY) {
		this.positionY = positionY;
	}

	public int getRadius() {
		return radius;
	}

	public void setRadius(int radius) {
		this.radius = radius;
	}

	public CountDownTimer getReloadTimer() {
		return reloadTimer;
	}

	public void setReloadTimer(CountDownTimer reloadTimer) {
		this.reloadTimer = reloadTimer;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public  void shoot()
	{
		Monster target = inRangeMonster();
		if (target != null)
		{
			target.removeHealth(this.damage);
			if(target.getHealth() <= 0)
			{
				
				
				gameboard[target.getPosition().getX()][target.getPosition().getY()] =   new GameObject(GameView.PATH);
				target.Destroy();
				target = null;
				
				
			}
		}
		getReloadTimer().start();
	}
	
	public int sell()
	{
		gameboard[positionX][positionY] = new GameObject(GameView.CELL);
		reloadTimer.cancel();
		return (int)( value * 0.75);
	}
	
	public abstract void upgrade();

	public int getDamage() {
		return damage;
	}

	public void setDamage(int damage) {
		this.damage = damage;
	}
	public Monster inRangeMonster()
	{
		
		for(Path p : this.rangePath)
		{
			if(gameboard[p.getX()][p.getY()] instanceof Monster)
			{
				return (Monster) gameboard[p.getX()][p.getY()];
			}
		}
		
		
		return null;
	}
	

	
}

